<?php
class ModelToolImage extends Model {
	public function resize($filename, $width = 100, $height = 100, $placeholder = true) {
        if (defined('IMAGE_BASE_URL')){
            return $this->resizeCloudImage($filename, $width, $height);
        }

		if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != str_replace('\\', '/', DIR_IMAGE)) {
			if ($placeholder) {
				$filename = 'no_image.png';
			} else {
				return;
			}
		}

		$extension = pathinfo($filename, PATHINFO_EXTENSION);

		$image_old = $filename;
		$image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;

		if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) {
			list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);

			if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) {
				return DIR_IMAGE . $image_old;
			}

			$path = '';

			$directories = explode('/', dirname($image_new));

			foreach ($directories as $directory) {
				$path = $path . '/' . $directory;

				if (!is_dir(DIR_IMAGE . $path)) {
					@mkdir(DIR_IMAGE . $path, 0777);
				}
			}

			if ($width_orig != $width || $height_orig != $height) {
				$image = new Image(DIR_IMAGE . $image_old);
				$image->resize($width, $height);
				$image->save(DIR_IMAGE . $image_new);
			} else {
				copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
			}
		}

		$image_new = str_replace(' ', '%20', $image_new);  // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +

		return HTTP_CATALOG . 'image/' . $image_new;
	}

    public function resizeCloudImage($filename, $width = 100, $height = 100)
    {
        $file_url = is_int(strpos($filename,'http')) ? $filename : IMAGE_BASE_URL . $filename;
        $file_url = explode('?', $file_url)[0];

        if (CK_FINDER_BACKEND === 'oss') {
            /**
             * @link https://www.alibabacloud.com/help/zh/doc-detail/44688.htm?spm=a2c63.p38356.b99.573.731d4733tptyhm
             */
            return $file_url . "?x-oss-process=image/resize,w_$width,h_$height,limit_0";
        } elseif (CK_FINDER_BACKEND === 'bos') {
            return $file_url . "@w_$width,h_$height";
        } else {
            return null;
        }
    }

    /**
     * @param $filename
     * @return null|string
     */
    public function getVideo($filename)
    {
        if (defined('IMAGE_BASE_URL')){
            return $this->getCloudVideo($filename);
        }

        if (is_file(DIR_IMAGE . $filename)) {
            return DIR_IMAGE . $filename;
        } else {
            return null;
        }

    }

    public function getCloudVideo($filename)
    {
        if (CK_FINDER_BACKEND === 'oss') {
            /**
             * @link https://www.alibabacloud.com/help/zh/doc-detail/44688.htm?spm=a2c63.p38356.b99.573.731d4733tptyhm
             */
            return IMAGE_BASE_URL . $filename;
        } elseif (CK_FINDER_BACKEND === 'bos') {
            return IMAGE_BASE_URL . $filename;
        } else {
            return null;
        }
    }
}
